package com.zz.parkingsystem.controller.user;

import com.zz.parkingsystem.entity.Order;
import com.zz.parkingsystem.entity.OrderItem;
import com.zz.parkingsystem.service.OrderItemService;
import com.zz.parkingsystem.service.OrderService;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import com.zz.parkingsystem.vo.OrderVO;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@ResponseBody
@Controller
@RequestMapping("/order")
@Api(value = "",tags = "订单接口")
public class UserOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;
    //使用中
    //用户登录完成后，根据用户id查询该用户是否有未支付订单
    @GetMapping("/selByUserIdAndUnpaid/{userId}")
    public Result selOrderItemByUserIdAndUnpaid(@PathVariable(value = "userId") Long userId){
        OrderItem orderItem = orderItemService.selOrderItemByUserIdAndUnpaid(userId);
        if(orderItem==null){
            return ResultGenerator.genSuccessResult("未有需处理订单，信誉良好，请继续保持");
        }
        return ResultGenerator.genFailResult("您有未处理的订单，请及时付款");
    }
    //不用了
    //用户停车后，输入泊位号停车，或者扫码停车（认领订单）
    @PostMapping("/addOrder")
    public Result addOrder(@RequestBody Order order){
        order.setOrderStatus(0);//进行中
        orderService.insertSelectiveUser(order);
        return ResultGenerator.genSuccessResult("用户认领订单成功，订单生成");
    }
    //用户认领订单后，给前端展示的页面
    @GetMapping("/showAfterAddOrder/{OiNo}")
    public Result showAfterAddOrder(@PathVariable String OiNo){
        OrderVO orderVO = orderService.selOrderVOByOiNo(OiNo);
        return ResultGenerator.genSuccessResult(orderVO);
    }
    //使用中
    //用户支付订单，修改订单状态为已支付，订单添加支付时间，支付方式，生成对应的资金流水,对应的账户表消费金额加，未支付金额减
    @PutMapping("payOrder/{oiNo}")
    public Result payOrder(@PathVariable String oiNo){
        //前端返回showVO中的oiNo(订单流水号)
        Order order = orderService.selOrderByOiNo(oiNo);
        orderService.updateByPrimaryKeySelectiveUser(order);
        return ResultGenerator.genSuccessResult("订单支付成功");
    }
}
